package com.dys.hejiayuncommunity.web.controller.system;

import com.dys.hejiayuncommunity.common.constant.UserConstants;
import com.dys.hejiayuncommunity.common.core.controller.BaseController;
import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import com.dys.hejiayuncommunity.common.core.page.PageResult;
import com.dys.hejiayuncommunity.common.utils.SecurityUtils;
import com.dys.hejiayuncommunity.common.utils.ServletUtils;
import com.dys.hejiayuncommunity.framework.service.SysPermissionService;
import com.dys.hejiayuncommunity.system.domain.LoginUser;
import com.dys.hejiayuncommunity.system.domain.SysRole;
import com.dys.hejiayuncommunity.system.service.SysRoleService;
import com.dys.hejiayuncommunity.system.service.SysUserService;
import com.dys.hejiayuncommunity.system.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: 疾风
 * @ClassName: SysRoleController
 * @Description: 角色信息
 * @CreateDate: 2024/12/31
 * @Version: v1.0.0
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private SysUserService userService;

    /**
     * 获取角色列表
     */
    @GetMapping("/list")
    public PageResult list(SysRole role) {
        super.startPage();
        List<SysRole> roleList = roleService.selectRoleList(role);
        return super.getData(roleList);
    }

    /**
     * 获取角色信息
     */
    @GetMapping("/{roleId}")
    public BaseResponse getInfo(@PathVariable Long roleId) {
        return BaseResponse.success(roleService.selectRoleById(roleId));
    }

    /**
     * 修改角色状态
     */
    @PutMapping("/changeStatus")
    public BaseResponse changeStatus(@RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);
        role.setUpdateBy(SecurityUtils.getUserName());
        return super.toAjax(roleService.updateRoleStatus(role));
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{roleIds}")
    public BaseResponse remove(@PathVariable Long[] roleIds) {
        return super.toAjax(roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 获取角色选择列表
     */
    @GetMapping("/optionselect")
    public BaseResponse optionSelect() {
        return BaseResponse.success(roleService.selectRoleAll());
    }

    /**
     * 添加角色
     */
    @PostMapping
    public BaseResponse add(@RequestBody SysRole role) {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return BaseResponse.failed("新增角色失败," + role.getRoleName() + "角色名称已经存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return BaseResponse.failed("新增角色失败," + role.getRoleKey() + "角色权限已经存在");
        }
        role.setCreateBy(SecurityUtils.getUserName());
        return super.toAjax(roleService.insertRole(role));
    }

    /**
     * 修改角色
     */
    @PutMapping
    public BaseResponse edit(@RequestBody SysRole role) {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return BaseResponse.failed("修改角色失败," + role.getRoleName() + "角色名称已经存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return BaseResponse.failed("修改角色失败," + role.getRoleKey() + "角色权限已经存在");
        }
        role.setUpdateBy(SecurityUtils.getUserName());
        if (roleService.updateRole(role) > 0) {
            // 更新缓存用户权限
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            if (!Objects.isNull(loginUser) && !loginUser.getSysUser().isAdmin()) {
                loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getSysUser()));
                loginUser.setSysUser(userService.findUserByUsername(loginUser.getSysUser().getUserName()));
                tokenService.setLoginUser(loginUser);
            }
            return BaseResponse.success("修改角色信息成功");
        }
        return BaseResponse.failed("修改" + role.getRoleName() + "失败,请联系管理员");
    }
}
